package view.Windows;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class UpdateDBGUI {

	private final static UpdateDBGUI INSTANCE = new UpdateDBGUI();

	public static UpdateDBGUI get() {
		return INSTANCE;
	}

	public UpdateDBGUI() {
	}

	protected Shell shlMain;
	protected Color cBackgroundColor;
	protected Font fHeadFont;
	protected Font fLabelFont;
	protected Font fButtonFont;
	protected ProgressBar pbDBUpdate;
	protected Display mainDisp;

	public void UpdateDBGUIRun(final Display mainDisp) {

		setMainDisp(mainDisp);
		initFonts(mainDisp);

		shlMain = new Shell(mainDisp, SWT.SHELL_TRIM & ~SWT.RESIZE & SWT.CLOSE & ~SWT.MAX);
		shlMain.setBounds(10, 10, 400, 250);

		// locate the shell in the center of the display
		Monitor primary = mainDisp.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shlMain.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shlMain.setLocation(x, y);

		// main definitions
		shlMain.setText("LegenDate - Update DB!");
		Image icon = new Image(mainDisp, getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDate.png"));
		shlMain.setImage(icon);
		Color clrWhite = new Color(mainDisp, 255, 255, 255);
		Image imgBackground = new Image(mainDisp, getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDateBG2.jpg"));
		shlMain.setBackgroundImage(imgBackground);
		shlMain.setBackgroundMode(SWT.INHERIT_DEFAULT);

		Label lblName = new Label(shlMain, SWT.LEFT);
		lblName.setText("Updating DB from FreeBase...");
		lblName.setBounds(60, 40, 210, 20);
		lblName.setForeground(clrWhite);

		pbDBUpdate = new ProgressBar(shlMain, SWT.INDETERMINATE);
		pbDBUpdate.setBounds(50, 90, 300, 30);

		shlMain.open();
		// this is what's holding the shell open!!!
		while (!shlMain.isDisposed()) {
			if (!mainDisp.readAndDispatch())
				mainDisp.sleep();
		}
	}

	public Color getBackgroundColor() {
		return cBackgroundColor;
	}

	public void setBackgroundColor(Color cBackgroundColor) {
		this.cBackgroundColor = cBackgroundColor;
	}

	public Font getHeadFont() {
		return fHeadFont;
	}

	public void setHeadFont(Font fHeadFont) {
		this.fHeadFont = fHeadFont;
	}

	public Font getLabelFont() {
		return fLabelFont;
	}

	public void setLabelFont(Font fLabelFont) {
		this.fLabelFont = fLabelFont;
	}

	public Font getButtonFont() {
		return fButtonFont;
	}

	public void setButtonFont(Font fButtonFont) {
		this.fButtonFont = fButtonFont;
	}

	public Display getMainDisp() {
		return mainDisp;
	}

	public void setMainDisp(Display mainDisp) {
		this.mainDisp = mainDisp;
	}

	/**
	 * initialize fonts
	 * 
	 * @param display
	 */
	protected void initFonts(Display display) {
		setHeadFont(new Font(display, new FontData("Fiolex Girls", 20, SWT.BOLD)));
		setLabelFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));
		setButtonFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));

	}

	// close the popup (when the update is over)
	public void closePopup() {
		shlMain.close();
		shlMain.dispose();
	}

}
